package me.firstapp.api.shop;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.shop.DistributeCall;
import me.firstapp.module.shop.Order;
import me.firstapp.module.shop.OrderItem;
import me.firstapp.module.shop.PayRecord;
import me.firstapp.service.shop.DistributeCallService;
import me.firstapp.service.shop.OrderItemService;
import me.firstapp.service.shop.OrderService;
import me.firstapp.service.shop.PayRecordService;

@Controller
@RequestMapping(value = "/order")
@Api(value = "/order", description = "订单相关接口")
public class OrderApi extends BaseApi {

	@Autowired
	private OrderService orderService;

	@Autowired
	private OrderItemService orderItemService;

	@Autowired
	private PayRecordService payRecordService;

	@Autowired
	private DistributeCallService distributeCallService;

	@RequestMapping(value = "/createOrder.do", method = RequestMethod.POST)
	@ApiOperation(value = "创建订单", notes = "创建订单")
	public void createOrder(Long memberId, String remark, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Order> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Order order = orderService.addOrder(memberId, remark, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(order);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findOrderByOrderNo.do", method = RequestMethod.GET)
	@ApiOperation(value = "根据订单号获取订单", notes = "根据订单号获取订单")
	public void findOrderByOrderNo(String orderNo,
			@RequestParam(value = "itemsFlag", defaultValue = "false") boolean itemsFlag, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Order> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Order order = orderService.findOrderByOrderNo(orderNo, itemsFlag, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(order);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/payOrder.do", method = RequestMethod.POST)
	@ApiOperation(value = "积分支付订单", notes = "积分支付订单")
	public void payOrder(Long memberId, String orderNo, String password, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Order> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Order order = orderService.executePayOrder(memberId, orderNo, password, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(order);
			payRecordService.addPayRecord(orderNo, order.getValue(), apiClientId);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findMemberOrders.do", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取用户订单", notes = "分页获取用户订单")
	public void findMemberOrders(@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, Long memberId,
			Integer status, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Order> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<Order> orderPage = orderService.findOrderPage(pageNo, pageSize, memberId, status, apiClientId);
			JsonPage<Order> jsonPage = new JsonPage<Order>();
			jsonPage.setList(orderPage.getEntities());
			jsonPage.setPageNo(orderPage.getPageNo());
			jsonPage.setPageSize(orderPage.getPageSize());
			jsonPage.setTotalCount(orderPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/cancelOrder.do", method = RequestMethod.POST)
	@ApiOperation(value = "取消訂單", notes = "取消訂單")
	public void cancelOrder(String orderNo, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Order> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Order order = orderService.executeCancelOrder(orderNo, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(order);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/completeOrder.do", method = RequestMethod.POST)
	@ApiOperation(value = "完成訂單", notes = "完成訂單")
	public void completeOrder(String orderNo, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Order> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Order order = orderService.executeCompleteOrder(orderNo, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(order);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/distributeOrder.do", method = RequestMethod.POST)
	@ApiOperation(value = "訂單发货", notes = "訂單发货")
	public void distributeOrder(String orderNo, String expressName, String expressNo, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Order> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Order order = orderService.executeDistributeOrder(orderNo, expressName, expressNo, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(order);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findOrderPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询订单", notes = "分页查询订单")
	public void findOrderPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, Long startTime, Long endTime, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Order> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Date startDate = null;
			if (startTime != null) {
				startDate = DateUtils.getDateFromTimeStamp(String.valueOf(startTime), "yyyy-MM-dd");
			}
			Date endDate = null;
			if (endTime != null) {
				endDate = DateUtils.getDateFromTimeStamp(String.valueOf(endTime), "yyyy-MM-dd");
			}
			Page<Order> orderPage = orderService.findOrderPage(pageNo, pageSize, searchParams, sortParam, sortType,
					startDate, endDate, apiClientId);
			JsonPage<Order> jsonPage = new JsonPage<Order>();
			jsonPage.setList(orderPage.getEntities());
			jsonPage.setPageNo(orderPage.getPageNo());
			jsonPage.setPageSize(orderPage.getPageSize());
			jsonPage.setTotalCount(orderPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findOrderItemPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询子订单", notes = "分页查询子订单")
	public void findOrderItemPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, Long orderId, HttpServletRequest request, HttpServletResponse response) {
		PageObject<OrderItem> result = new PageObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<OrderItem> orderPage = orderItemService.findOrderItemPage(pageNo, pageSize, searchParams, sortParam,
					sortType, orderId);
			JsonPage<OrderItem> jsonPage = new JsonPage<OrderItem>();
			jsonPage.setList(orderPage.getEntities());
			jsonPage.setPageNo(orderPage.getPageNo());
			jsonPage.setPageSize(orderPage.getPageSize());
			jsonPage.setTotalCount(orderPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findPayRecordPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询支付记录", notes = "分页查询支付记录")
	public void findPayRecordPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, Long startTime, Long endTime, HttpServletRequest request, HttpServletResponse response) {
		PageObject<PayRecord> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Date startDate = null;
			if (startTime != null) {
				startDate = DateUtils.getDateFromTimeStamp(String.valueOf(startTime), "yyyy-MM-dd");
			}
			Date endDate = null;
			if (endTime != null) {
				endDate = DateUtils.getDateFromTimeStamp(String.valueOf(endTime), "yyyy-MM-dd");
			}
			Page<PayRecord> payRecordPage = payRecordService.findPayRecordPage(pageNo, pageSize, searchParams,
					sortParam, sortType, startDate, endDate, apiClientId);
			JsonPage<PayRecord> jsonPage = new JsonPage<PayRecord>();
			jsonPage.setList(payRecordPage.getEntities());
			jsonPage.setPageNo(payRecordPage.getPageNo());
			jsonPage.setPageSize(payRecordPage.getPageSize());
			jsonPage.setTotalCount(payRecordPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findOrders.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询订单列表", notes = "查询订单列表")
	public void findOrders(Long memberId, Integer status, HttpServletRequest request, HttpServletResponse response) {
		ListObject<Order> result = new ListObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			List<Order> orders = orderService.findOrders(memberId, status, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setItems(orders);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "distributeCall.do", method = RequestMethod.POST)
	@ApiOperation(value = "订单发货提醒", notes = "订单发货提醒")
	public void addUpdateDistributeCall(Long memberId, String orderNo, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			distributeCallService.addUpdateDistributeCall(memberId, orderNo, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findDistributeCallPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询订单发货提醒", notes = "分页查询订单发货提醒")
	public void findDistributeCallPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<DistributeCall> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<DistributeCall> callPage = distributeCallService.findDistributeCallPage(pageNo, pageSize, searchParams,
					sortParam, sortType, apiClientId);
			JsonPage<DistributeCall> jsonPage = new JsonPage<DistributeCall>();
			jsonPage.setList(callPage.getEntities());
			jsonPage.setPageNo(callPage.getPageNo());
			jsonPage.setPageSize(callPage.getPageSize());
			jsonPage.setTotalCount(callPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "handleDistributeCall.do", method = RequestMethod.POST)
	@ApiOperation(value = "处理订单发货提醒", notes = "处理订单发货提醒")
	public void handleDistributeCall(String orderNo, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			distributeCallService.executeHandleCall(orderNo, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
